<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/10/8 0008
  Time: 下午 7:11
  To change this template use File | Settings | File Templates.
--%>
<script type="text/javascript" src="/js/jquery-1.8.3.js"></script>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新增试卷</title>
</head>
<style>
    div{
        float: left;
    }
    .x{
        width: 100%;
    }
    #control{
        margin-top: 30px;
        margin-left: 30%;
        margin-bottom: 30px;
    }
    a{
        text-decoration: none;
        color: darkmagenta;
    }
    .pages{
        padding-left: 20px;
    }
    #selectbtn{
        margin-left: 40%;
        margin-top: 30px;
        margin-bottom: 30px;
    }
    input[type="button"]{
        border: 0px solid;
        width: 70px;
        height: 30px;
    }
    .exammsg{
        background-color: #E0FFFF;
        border: 1px solid grey;
        height: 30px;
    }
    .addlabel{
        text-align: right;
    }
    .hh{
        line-height: 25px;
    }
</style>

<body>
<div id="addshowTk" style="width: 50%;height: 100%;background: lightblue;overflow: auto">
    <div id="selectbtn">
        <input type="button" style="background-color: dodgerblue;color: white" id="xztk" code="1" value="选择题">&nbsp;&nbsp;&nbsp;
        <input type="button" style="background-color: dodgerblue;color: white" id="pdtk" code="2" value="判断题">
    </div>
    <input type="hidden" id="ezid" value="${zid}">
    <input type="hidden" id="ye" value="">
    <table id="tk" style="width: 100%;">
        <th width="700px" style="width: 70%;">试题</th><th style="width: 15%;">答案</th><th style="width: 15%;">操作</th>

    </table>
    <div id="control">
        <a href="javascript:void(0)" id="first" class="pages"><strong>首页</strong></a>
        <a href="javascript:void(0)" id="prev" class="pages"><strong>上一页</strong></a>
        <a href="javascript:void(0)" id="next" class="pages"><strong>下一页</strong></a>
        <a href="javascript:void(0)" id="last" class="pages"><strong>尾页</strong></a><br/>
    </div>
</div>
<div id="addExam" style="width: 50%;height: 100%;background-color: powderblue;overflow: auto">
    <input type="hidden" id="eyid" value="${yid}">
    <input type="hidden" id="th" value="${teacherid}">
    <input type="hidden" id="ttkk" >
    <input type="hidden" id="estring" >
    <input type="button" id="fh" value="返回" style="margin: 1% 30px;background-color: dodgerblue;color: white" onclick="javascript:history.back(-1);">
    <input type="button" id="button2" value="提交试卷" style="margin: 1% 30px;margin-left: 25%;background-color: #5eb95e;color:white;"><br/>
    <table cellpadding="10px">
        <tr>
            <td class="addlabel"><strong>试卷名：</strong></td>
            <td><input type="text" id="sjm" value="" class="exammsg" size="33px" ></td>
        </tr>
        <tr>
            <td class="addlabel"><strong>考试的开始时间：</strong></td>
            <td><input type="datetime-local" id="kstime" value="" class="exammsg"></td>
        </tr>
        <tr>
            <td class="addlabel"><strong>考试的结束时间：</strong></td>
            <td><input type="datetime-local" id="jstime" value="" class="exammsg"></td>
        </tr>
        <tr>
            <td class="addlabel"><strong>考试时间：</strong></td>
            <td><input type="text" id="tm" value="" size="33px" class="exammsg">分钟</td>
        </tr>
        <tr>
            <td class="addlabel"><strong>考试的年级：</strong></td>
            <td>
                <select id="nj" style="width:100px"  class="exammsg">
                    <option>--选择年级--</option>
                    <c:forEach items="${list}" var="li">
                    <option value="${li.gId}">${li.gId}</option>
                    </c:forEach>
                </select>
            </td>
        </tr>
    </table>
    <br/>

    <table id="xz" style="width: 100%;">
        <tr><td colspan="3"><h3>选择题</h3></td></tr>
    </table><br/><br/>

    <table id="pd" style="width: 100%;">
        <tr><td colspan="3"><h3>判断题</h3></td></tr>
    </table>
</div>
<script>
    var currPage;
    var tPages;

    $(function () {
        /**
        * 遍历题库
        * */
        var getaddTk = function (curr,m) {
            $.ajax({
                type:"POST",
                url:"/teacher/addEshowtkServlet",
                dataType:"JSON",
                data:{
                    pageSize:15,
                    pageNum:curr||1,
                    zid:$("#ezid").val(),
                    code:$("#ye").val()||1,
                },
                success:function (data) {
                    $("#tk").find("tr[class='tks']").remove();
                    $.each(data.List,function (index,obj){
                        if (data.styleid==1){
                            var $tks= "<tr class='tks'>" +
                                "<td colspan='3'>" +
                                "<table class='x' border='0'>" +
                                "<tr class='hh'>" +
                                "<td width='70%'>" +((m||0)+index+1)+"、"+obj.tN+ "</td>" +
                                "<td rowspan='5' width='15%' align='center' >" +"&nbsp;&nbsp;"+obj.ndAn+ "</td>" +
                                "<td rowspan='5' width='15%' align='center'><input type='button' style='background-color: dodgerblue;color: white' class='b1'  code='"+obj.tId+"' value='添加'>" + "</td>" +
                                "</tr>" +
                                "<tr class='hh'>" + "<td>" +obj.aN+ "</td>" + "</tr>" +
                                "<tr class='hh'>" + "<td>" +obj.bN+ "</td>" + "</tr>" +
                                "<tr class='hh'>" + "<td>" +obj.cN+ "</td>" + "</tr>" +
                                "<tr class='hh'>" + "<td>" +obj.dN+ "</td>" + "</tr>" +
                                "</table>" +
                                "<hr/></td>" +
                                "</tr>" ;
                        }else {
                            var $tks="<tr class='tks'>" +
                                "<td colspan='3'>" +
                                "<table class='x' border='0'>" +
                                "<tr class='hh'>" +
                                "<td width='70%'>" +((m||0)+index+1)+"、"+obj.tM+ "</td>" +
                                "<td rowspan='1' width='15%' align='center'>" +"&nbsp;&nbsp;" +obj.mdAn+ "</td>" +
                                "<td rowspan='1' width='15%' align='center'><input type='button' style='background-color: dodgerblue;color: white' class='b1' code='"+obj.tId+"' value='添加'>" + "</td>" +
                                "</tr>" +
                                "</table>" +
                                "<hr/></td>" +

                                "</tr>" ;
                        }
                        $("#tk").append($tks);
                    });

                    currPage=data.pageNum;
                    tPages=data.total;

                    $("#tk").find("input[class='b1']").click(function () {
                        code = $(this).attr("code");
                        gettidval(code);
                        tdz = $("#ttkk").val();
                        es = $("#estring").val();
                        getAdd(tdz,es,0);
                    });
                }
            });
        }
        /*分页*/
        $(".pages").click(function () {
            var flag=$(this).attr("id");
            if (flag=="first"){
                getaddTk(1,0);
            } else if (flag=="prev"){
                currPage--;
                if (currPage<1){
                    currPage++;
                }
                n=(currPage-1)*15
                getaddTk(currPage,n);
            } else if (flag=="next"){
                currPage++;
                if (currPage>tPages){
                    currPage--;
                }
                n=(currPage-1)*15;
                getaddTk(currPage,n);
            } else if (flag=="last"){
                n=(tPages-1)*15;
                getaddTk(tPages,n);
            }
        });
        var gettidval = function (code) {
                $("#ttkk").val(code);
        }

        /**
        * 向试卷中添加试题或删除试卷中的试题
        * */
        var getAdd = function (tdz,es,tdy) {
            $.ajax({
                type:"POST",
                url:"/teacher/addExamServlet_action",
                dataType:"JSON",
                data:{
                    tid:tdz||0,
                    estring:es,
                    tdy:tdy||0,
                },
                success:function (data) {
                    getesval(data);
                    getaddshowExam(data);
                }
            });
        }

        var getesval = function (data) {
            $("#estring").val(data);
        }

        /**
         * 显示试卷中的试题
         * */
        var getaddshowExam = function (estring) {
            $.ajax({
                type:"POST",
                url:"/teacher/UpExamServlet",
                dataType:"JSON",
                data:{
                    estring:estring,
                },
                success:function (data) {
                    $("#xz").find("tr[class='xzs']").remove();
                    $("#pd").find("tr[class='pds']").remove();
                    $.each(data.xzdList,function (index,obj) {
                        var $xztr= "<tr class='xzs'>" +
                            "<td colspan='3'>" +
                            "<table class='x'>" +
                            "<tr class='hh'>" +
                            "<td width='70%'>" +(index+1)+"、"+obj.tN+ "</td>" +
                            "<td rowspan='5' width='15%' align='center'>" +"&nbsp;&nbsp;"+obj.ndAn+ "</td>" +
                            "<td rowspan='5' width='15%' align='center'><input type='button' style='background-color: crimson;color: white' class='a' td='"+obj.tId+"' value='删除'>" + "</td>" +
                            "</tr>" +
                            "<tr class='hh'>" + "<td>" +obj.aN+ "</td>" + "</tr>" +
                            "<tr class='hh'>" + "<td>" +obj.bN+ "</td>" + "</tr>" +
                            "<tr class='hh'>" + "<td>" +obj.cN+ "</td>" + "</tr>" +
                            "<tr class='hh'>" + "<td>" +obj.dN+ "</td>" + "</tr>" +
                            "</table>" +
                            "<hr/></td>" +
                            "</tr>" ;
                        $("#xz").append($xztr);
                    });
                    $.each(data.pddList,function (index,o) {
                        var $pdtr=" <tr class='pds'>" +
                            "<td colspan='3'>" +
                            "<table class='x'>" +
                            "<tr class='hh'>" +
                            "<td width='70%'>" +(index+1)+"、"+o.tM+ "</td>" +
                            "<td rowspan='1' width='15%' align='center'>" +"&nbsp;&nbsp;" +o.mdAn+ "</td>" +
                            "<td rowspan='1' width='15%' align='center'><input type='button'  style='background-color: crimson;color: white' class='b' td='"+o.tId+"' value='删除'>" + "</td>" +
                            "</tr>" +
                            "</table>" +
                            "<hr/></td>" +
                            "</tr>" ;
                        $("#pd").append($pdtr);
                    });
                    $("#xz").find("input[class='a']").click(function () {
                        es=$("#estring").val();
                        td=$(this).attr("td");
                        getAdd(0,es,td);
                    });
                    $("#pd").find("input[class='b']").click(function () {
                        esz=$("#estring").val();
                        tdy=$(this).attr("td");
                        getAdd(0,esz,tdy);
                    });
                }
            });
        }
        /**
         * 点击生成选择或判断题
         * */
        $("#xztk").click(function () {
            var coe=$(this).attr("code");
            $("#ye").val(coe);
            getaddTk();
        });
        $("#pdtk").click(function () {
            var coe=$(this).attr("code");
            $("#ye").val(coe);
            getaddTk();
        });
        getaddTk();

        /**
         * 点击提交试卷
         * */
        $("#button2").click(function () {
            getQR($("#th").val(),$("#ezid").val(),$("#sjm").val(),$("#estring").val(),$("#kstime").val(),$("#jstime").val(),$("#tm").val(),$("#eyid").val(),$("#nj").val());
        });
        /**
         * 提交修改的试卷
         * */
        var getQR = function (th,ez,sj,esz,kt,jt,tm,ey,nj) {
            if (th==""||ez==""||sj==""||esz==""||kt==""||jt==""||tm==""||ey==""||nj=="") {
                alert("有主要信息缺失，请补充");
                getaddshowExam();
            }else {
                $.ajax({
                    type:"POST",
                    url:"/teacher/QRaddServlet",
                    data:{
                        teacherid:th,
                        zid:ez,
                        examname:sj,
                        estring:esz,
                        estart:kt,
                        eend:jt,
                        etime:tm,
                        yid:ey,
                        gid:nj,
                    },
                    success:function (data) {
                        if (data > 0){
                            alert("提交成功");

                        } else {
                            alert("提交失败");
                        }
                    }
                });
            }
        }
    });
</script>
</body>
</html>
